package java_常用api;

public class API_5_高级数组操作 {
    public static void main(String[] args) {
    // 基本查找
        int[] list = {1,2,3,4,5,6,7,8,9,10};
        int x = 3;


        //二分 查找   (必须按 顺序排序)
        int index = two_cat(list,x);
        System.out.println(index);
    }

    private static int two_cat(int[] list, int x) {
        // 定义查找的范围
        int min = 0;
        int max = list.length-1;
        while (min <= max){
            int mid = (min+max)/2;

            // mid > 数据
            if (list[mid] > x){
                //数据在左边
                max = mid-1;
            }

            // mid < 数据
            if (list[mid] < x){
                //数据在右
                min = mid+1;
            }
            // mid = 数据
            if (list[mid] == x){
                return  mid;
            }
        }
        // 如果 min <= max
        return -1;
    }
}
